﻿using Logistics.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.DataAccess
{
    public class tc_Role
    {
        public bool Add(Model.tc_Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO TC_ROLES(");
            strSql.Append("ID,ROLE)");
            strSql.Append(" VALUES (");
            strSql.Append("@in_ID,@in_ROLE)");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_ID", DbType.String, model.Id.ToString().Length ,model.Id.ToString()),
                DBHelper.CreateDbParameter("@in_ROLE", DbType.String,50, DBValue.GetDBValue(model.Role))};

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public bool Update(Model.tc_Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TC_ROLES SET ");
            strSql.Append("ROLE=@in_ROLE");
            strSql.Append(" WHERE ID=@in_ID");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_ROLE", DbType.String,50,  DBValue.GetDBValue(model.Role)),
                DBHelper.CreateDbParameter("@in_ID", DbType.String, model.Id.ToString().Length ,model.Id.ToString())};

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }

        public Model.tc_Role GetModel(Guid id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM TC_ROLES ");
            strSql.Append(" WHERE ID=@in_ID");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_ID", DbType.String, id.ToString().Length ,id.ToString())};

            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), cmdParms))
            {
                if (dr.Read())
                {
                    return GetModel(dr);
                }
                return null;
            }
        }

        public bool Delete(Guid id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TC_ROLES ");
            strSql.Append(" WHERE ID=@in_ID");
            OracleParameter[] cmdParms = {
                DBHelper.CreateDbParameter("@in_ID", DbType.String,  id.ToString().Length,id.ToString())};

            return DBHelper.ExecuteSql(strSql.ToString(), cmdParms) > 0;
        }


        public List<Model.tc_Role> GetList()
        {
            StringBuilder strSql = new StringBuilder("SELECT * FROM TC_ROLES");
            using (DbDataReader dr = DBHelper.ExecuteReader(strSql.ToString()))
            {
                List<Model.tc_Role> lst = GetList(dr);
                return lst;
            }
        }



        #region 
        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.tc_Role GetModel(DbDataReader dr)
        {
            Model.tc_Role model = new Model.tc_Role();
            model.Id = DBHelper.GetGuid(dr["ID"]);
            model.Role = DBHelper.GetString(dr["ROLE"]);
            return model;
        }

        /// <summary>
        /// 由DbDataReader得到泛型数据列表
        /// </summary>
        private List<Model.tc_Role> GetList(DbDataReader dr)
        {
            List<Model.tc_Role> lst = new List<Model.tc_Role>();
            while (dr.Read())
            {
                lst.Add(GetModel(dr));
            }
            return lst;
        }
        #endregion
    }
}
